#!/usr/bin/env python3.8
import os

def getCompleteItems(result):
    if len(result) == 0:
        return []
    maxlen = 0
    maxlen = len(max(result.keys(), key=len)) + 1
    ret = []
    for key in result:
        item = {}
        item["word"] = key
        item["abbr"] = key.ljust(maxlen) + result[key]
        ret.append(item)
    return ret


def suggest(word):
    start_flag = False
    end_flag = False
    begin_of_word = ""
    result = {}
    abs_path = os.path.dirname(__file__)
    if len(word) > 0 : 
        begin_of_word = word[0].lower()
    else:
        return result

    with open(abs_path + "/en_ch.dict", "r") as f:
        line = f.readline()
        while not end_flag and line != "":
            if line.startswith(word.lower()):
                word_sens = line.split()
                s_word = word + word_sens[0][len(word):]
                s_sens = "".join(word_sens[1:])
                result[s_word] = s_sens
                start_flag = True

            if start_flag and not line.startswith(begin_of_word):
                end_flag = True
            line = f.readline()
    return result
